﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Web.Mvc;
using Owlpal.Blogs.Models;

namespace Owlpal.Blogs.ModelBinders
{
    public class BlogBinder : IModelBinder
    {
        private static string blogCacheKey = "blog";
        public object BindModel(ControllerContext controllerContext, 
                            ModelBindingContext bindingContext)
        {
            if (bindingContext.Model != null)
                throw new InvalidOperationException("Cannot update instances");
            Owlpal.Blogs.Models.Blog blog = (Owlpal.Blogs.Models.Blog)controllerContext.HttpContext.Cache[blogCacheKey];
            if (blog == null)
            {
                blog = new Owlpal.Blogs.Models.Blog();
                controllerContext.HttpContext.Cache[blogCacheKey] = blog;
            }
            return blog; 
        }
    }
}
